package com.hzqy.service.itf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.RegionalPricingUtil;
import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.commons.CommonsDao;
import com.hzqy.persistence.dao.hqjc.HqjcDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.PmsOrgVo;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.SysProductVo;

@Service
public class HotelItfServiceBean extends GenericServiceBean<PmsUserVo, CommonsDao, Integer>{

	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private CommonsDao commonsDao;
	@Override
	public CommonsDao getEntityDao() {
		return commonsDao;
	}
	
	/**
	 * 拿到产品包价格
	 */
	public Map<String, Object> getProduct(String sign, String serverid, String timestamp, String secret, String source){
		Map<String, Object> map = new HashMap<String, Object>();
		int resultCode = 0;
		String resultMsg = "";
		if(sign == null||serverid == null||timestamp == null||secret == null||source == null){
			resultCode = 1001;//参数不全
			resultMsg = "参数不全";
		}else{
			String source_secret = SystemConfigUtils.getSysConfig(source+"_secret");
			Map<String, Object> signParams = new HashMap<String, Object>();
			LOG.debug("serverid："+serverid);
			LOG.debug("timestamp："+timestamp);
			LOG.debug("secret："+source_secret);
			LOG.debug("source："+source);
			signParams.put("serverid", serverid);
			signParams.put("timestamp", timestamp);
			signParams.put("secret", source_secret);
			signParams.put("source", source);
			String signStr = RegionalPricingUtil.getSign(signParams,source_secret);
			LOG.debug("接收到的sign："+sign);
			LOG.debug("本地获取的sign："+signStr);
			LOG.debug("接收到的secret："+secret);
			LOG.debug("本地获取的secret："+source_secret);
			if(signStr.equals(sign)&&source_secret.equals(secret)){//验证成功
				Map<String, Object> message = new HashMap<String, Object>();
				message.put("serverid", serverid);
				resultCode = 1000;//成功
				resultMsg = "请求成功";
				int catalogIdArea = 245;//区域定价账号分类id
				int catalogId = 15;//业务账号分类id
				PmsOrgVo org = new PmsOrgVo();
				List<PmsOrgVo> resultArea = commonsDao.selectOrgByContentAndCatalogId(serverid,catalogIdArea);
				List<PmsOrgVo> result = commonsDao.selectOrgByContentAndCatalogId(serverid,catalogId);
				if(!resultArea.isEmpty()){
					org = resultArea.get(0);
				}else if(!result.isEmpty()){//区域定价账号分类内没有对应账号
					org = result.get(0);
				}
				if(org==null){//账号没有对应的酒店
					int defaultProductId = Integer.valueOf(SystemConfigUtils.getSysConfig("defaultProductId"));
					Map<String,Object> map_f_sp_id = new HashMap<String, Object>();
					map.put("f_sp_id", defaultProductId);
					List<SysProductVo> productVo = commonsDao.selectProductList(map_f_sp_id);
					message.put("price", productVo.get(0).getF_sp_fee());
				}else{
					if(org.getF_po_price()==0){
						Map<String, Object> productMap = new HashMap<String, Object>();
						productMap.put("f_sp_priority", 1);
						productMap.put("f_spo_orgid", org.getF_po_id());
						List<SysProductVo> spList = commonsDao.selectProductList(productMap);
						if(spList.size()==0){//酒店没有对应的产品包
							int defaultProductId = Integer.valueOf(SystemConfigUtils.getSysConfig("defaultProductId"));
							Map<String,Object> map_f_sp_id = new HashMap<String, Object>();
							map_f_sp_id.put("f_sp_id", defaultProductId);
							List<SysProductVo> productVo = commonsDao.selectProductList(map_f_sp_id);
							message.put("price", productVo.get(0).getF_sp_fee());
						}else{
							message.put("price", spList.get(0).getF_sp_fee());
						}
					}else{
						message.put("price", org.getF_po_price());
					}
					message.put("name", org.getF_po_name());
					message.put("address", org.getF_po_address());
					message.put("stars", org.getF_po_stars());
				}
				map.put("message", message);
			}else{
				resultCode = 1002;//参数错误
				resultMsg = "参数错误";
			}
		}
		map.put("resultCode", resultCode);
		map.put("resultMsg", resultMsg);
		return map;
	}
	
}
